﻿<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录-集成ilab-x</title>
    <link rel="stylesheet" type="text/css" href="style/login.css" />
    <script src="js/jquery-3.1.1.min.js"></script>
    <script>
        $(document).ready(function () {
            var GetLabInfo={
                GetQueryString: function(){
                    var reg = new RegExp("(^|&)token=([^&]*)(&|$)");
                    var r = window.location.search.substr(1).match(reg);
                    if(r != null) return unescape(r[2]); return null;
                },

                /**
                 * 函数功能：获取实验用户信息及实验或预约实验唯一标识符
                 *
                 */
                _GetUserInfo : function() {
                    var host = "http://139.224.197.21:8081/openlab/";

                    var token= this.GetQueryString();
                    if(token==null){
                        console.log('token值为空');
                        toIndexPage();
                    }else{
                        this.Ajax({
                            url: host + "outer/getMessageByToken",
                            type: "GET",
                            data: {"token":token},
                            dataType: "json",
                            success: function (response) {
                                // 此处放成功后执行的代码
                                // 解析返回的字符串 转为json对象
                                var jsonStr = response;
                                
                                try{
                                    var JsonData = JSON.parse(decodeURIComponent(escape(window.atob(jsonStr.replace("-", "+").replace("_", "/")))));
                                    //  var JsonData = JSON.parse(window.atob(jsonStr));
                                    $("#eid").attr("value",JsonData.eId);
                                    $("#username").attr("value",JsonData.userId);
                                    $('#ilabname').attr("value",JsonData.name);
                                    console.log("解析JSON对象字串："+JSON.stringify(JsonData));
                                    console.log("解析eid成功："+JsonData.eId);
                                    //  console.log("解析username成功："+utf8to16(JsonData.name));
                                    console.log("解析username成功："+JsonData.name);


                                }catch (err) {
                                    console.log("解析字串错误："+jsonStr);
                                }finally {
                                    toIndexPage();
                                }




                                window.postMessage(jsonStr,'*');
                            },
                            fail: function (status) {
                                // 此处放失败后执行的代码
                                console.log(status)
                            }
                        });

                    }


                },
                Ajax:function(options) {
                    options = options || {};
                    options.type = (options.type || "GET").toUpperCase();
                    options.dataType = options.dataType || "json";
                    var params = window.btoa(JSON.stringify(options.data))

                    //创建xhr对象 - 非IE6
                    if (window.XMLHttpRequest) {
                        var xhr = new XMLHttpRequest();
                    } else { //IE6及其以下版本浏览器
                        var xhr = new ActiveXObject('Microsoft.XMLHTTP');
                    }
                    xhr.withCredentials = true;
                    //GET POST 两种请求方式
                    if (options.type == "GET") {
                        xhr.open("GET", options.url + "?" + 'param=' + params, true);
                        xhr.send(null);
                    } else if (options.type == "POST") {
                        xhr.open("POST", options.url, true);
                        //设置表单提交时的内容类型
                        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                        xhr.send(params);
                    }
                    //接收
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState == 4) {
                            var status = xhr.status;
                            if (status >= 200 && status < 300) {
                                options.success && options.success(xhr.responseText);
                            } else {
                                options.fail && options.fail(status);
                            }
                        }
                    }
                }

            };
            GetLabInfo._GetUserInfo();




            function guid() {
                return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                    var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
                    return v.toString(16);
                });
            }

            function rand(n)
            {
                var str = '';
                for(var i=0;i<n;i++)
                {
                    str += Math.floor(Math.random()*10)
                }
                return str
            }


            function utf8to16(str) {
                var out, i, len, c;
                var char2, char3;
                out = "";
                len = str.length;
                i = 0;
                while(i < len) {
                    c = str.charCodeAt(i++);
                    switch(c >> 4)
                    {
                        case 0: case 1: case 2: case 3: case 4: case 5: case 6:case7:
                        // 0xxxxxxx
                        out += str.charAt(i-1);
                        break;
                        case 12: case 13:
                        // 110x xxxx 10xx xxxx
                        char2 = str.charCodeAt(i++);
                        out += String.fromCharCode(((c & 0x1F) << 6) | (char2&0x3F));
                        break;
                        case 14:
                            // 1110 xxxx 10xx xxxx 10xx xxxx
                            char2 = str.charCodeAt(i++);
                            char3 = str.charCodeAt(i++);
                            out += String.fromCharCode(((c & 0x0F) << 12) |
                                ((char2 & 0x3F) << 6) |
                                ((char3 & 0x3F) << 0));
                            break;
                    }
                }
                return out;
            }

            function toIndexPage() {

                if($('#username').val() != "") {
                    var str = {
                        "eid": $('#eid').val(),
                        "username": $('#username').val(),
                        "password": $('#password').val(),
                        "ilabname": $('#ilabname').val()
                    };

                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: "/loginlabTo",
                        contentType: "application/json;charset=utf-8;",
                        data:JSON.stringify(str),
                        success: function (data) {
                            console.log(data);
                            window.location.href = "/optionList";

                        }
                    });
                }
                else{
                    /* //。如果教育部的平台没有正确的获取账号。使用默认的访客账号。guest  密码1 。*/
                  /*  var str = {
                        "eid": "xxxx",
                        "username": "ERP"+rand(10),
                        "password": "1",
                        "ilabname": "guest"
                    };*/
                    var str = {
                        "eid": "xxxx",
                        "username": "guest",
                        "password": "1",
                        "ilabname": "guest"
                    };


                    $.ajax({
                        type: "post",
                        dataType: "json",
                        url: "/loginlabTo",
                        contentType: "application/json;charset=utf-8;",
                        data:JSON.stringify(str),
                        success: function (data) {
                            console.log(data);
                            window.location.href = "/optionList";

                        }
                    });
                }
            }







        })





    </script>
</head>
<body>
<div class="waves"></div>
    <input id="eid" type="hidden" class="input">
     <input id="username" type="hidden" class="input">
    <input id="password" type="hidden" class="input" data-type="password">
     <input id="ilabname" type="hidden" class="input" data-type="password">

</div>
<div class="loginlab-wrap"><div class="loginlab-timeg"></div></div>

</body>
</html>